package yhao.demo.autocode.core;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.util.Map;

public class AutoCodeUtils {
    public static final String PARENT_VERSION = "1.5.1";

    public static String readResource(String resourceUri) throws IOException {
        Resource resource = new ClassPathResource(resourceUri);
        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String str;
        while((str = br.readLine()) != null){
            sb.append(str).append("\n");
        }
        return sb.toString();
    }

    public static String replacePlaceHolder(String context, Map<String,String> param){
        if(param!=null&&param.size()>0){
            for(Map.Entry<String,String> entry:param.entrySet()){
                context = context.replaceAll("\\{"+entry.getKey()+"}",entry.getValue());
            }
        }
        return context;
    }

    public static void makeFile(String parentPath,String fileName,String content) throws IOException {
        String filePath = parentPath + (parentPath.endsWith(File.separator)?"":File.separator) + fileName;
        File file = new File(filePath);
        file.createNewFile();
        OutputStreamWriter out = new OutputStreamWriter(
                new FileOutputStream(file), "UTF-8");
        out.write(content);
        out.close();
    }
    public static void deleteFile(String parentPath,String fileName) throws IOException {
        String filePath = parentPath + (parentPath.endsWith(File.separator)?"":File.separator) + fileName;
        File file = new File(filePath);
        if(file.exists()){
            file.delete();
        }
    }

}
